<?php

namespace app\admin\controller;

use think\Db;


class Slider extends Base
{
    public function getSlideList()
    {
        $pageId = input('pageId') ? input('pageId') : 1;
        $pageSize = input('pageSize') ? input('pageSize') : 10;
        $where = [];
        if (input('name')) {
            array_push($where, ['name', 'like', '%' . input('name') . '%']);
        }
        if (input('type') != null) {
            array_push($where, ['type', '=', input('type')]);
        }
        if (input('status') != null) {
            array_push($where, ['status', '=', input('status')]);
        }
        $res = Db::name('slider')->where($where)->paginate($pageSize, false, ['page' => $pageId]);
        $res ? $this->suc($res) : $this->err('获取轮播图列表出错');
    }

    /**修改轮播图状态*/
    public function changeSliderStatus()
    {
        $data = [];
        $data['id'] = input('id');
        $data['status'] = input('status');

        if (empty($data['id'])) {
            $this->err('缺少轮播图主键');
        }
        $res = Db::name('slider')->update($data);
        if ($res == 0) {
            $this->err('修改失败');
        } else {
            $this->suc($res);
        }
    }

    /**获取轮播图详情*/
    public function findSliderDetail()
    {
        $where = [];
        if (!input('id')) {
            $this->err('缺少轮播图主键');
        }
        $where['id'] = input('id');
        $res = Db::name('slider')
            ->where($where)
            ->find();
        $res ? $this->suc($res) : $this->err('没有找到轮播图详情');
    }

    /**下拉查询*/
    public function getSourceList()
    {
        if (empty(input('type'))) {
            $this->err('数据类型没有传');
        }
        $type = input('type');
        $res = [];

        switch ($type) {
            case 1:
                $res = Db::name('goods')->field('id,name')->select();
                break;
            case 2 :
                $res = Db::name('active')->where('status', '1')->field('id,name')->select();
                break;
            case 3 :
                $res = Db::name('shop')->where('shopStatus', '1')->field('id,name')->select();
                break;
        }
        $res ? $this->suc($res) : $this->err('获取数据失败');
    }

    /**保存轮播图详情*/
    public function editSliderDetail()
    {
        $data = [];
        input('id') ? $data['id'] = input('id') : $this->err('缺少轮播图编号！');
        input('name') ? $data['name'] = input('name') : $this->err('缺少轮播图名称！');
        input('type') ? $data['type'] = input('type') : $this->err('缺少轮播图类型！');
        input('srcId') ? $data['srcId'] = input('srcId') : $this->err('缺少资源Id！');
        if (input('imgUrl')) {
            $data['imgUrl'] = input('imgUrl');
        }
        if (input('status') == null) {
            $this->err('缺少轮播图状态！');
        }
        $data['status'] = input('status');
        $data['createTime'] = createTime();
        $res = Db::name('slider')->update($data);
        $res ? $this->suc($res) : $this->err('保存失败！');
    }

    /**新增轮播图*/
    public function addSlider()
    {
        $data = [];
        $data['name'] = input('name') ? input('name') : $this->err('缺少name');
        $data['type'] = input('type') ? input('type') : $this->err('缺少type');
        $data['srcId'] = input('srcId') ? input('srcId') : $this->err('缺少srcId');
        $data['sort'] = input('sort') ? input('sort') : $this->err('缺少sort');
        $data['imgUrl'] = input('imgUrl') ? input('imgUrl') : $this->err('imgUrl');
        $data['createTime'] = createTime();
        if (input('status') == null) {
            $this->err('缺少status');
        } else {
            $data['status'] = input('status');
        }

        $res = Db::name('slider')->insert($data);
        if ($res == 1) {
            $this->suc($res);
        } else if ($res == 0) {
            $this->err('新增轮播图失败');
        }
    }
}